<template>
  <!--赠送消费券-->
  <div class="addhuiyuan">
    <wv-group title="姓名">
      <wv-input placeholder="请输入姓名" v-model="employeeGiveGoods.userName"></wv-input>
    </wv-group>
    <wv-group title="手机号">
      <wv-input placeholder="请输入手机号" type="number" :maxlength="11"
                v-model="employeeGiveGoods.userMobile"></wv-input>
    </wv-group>
    <wv-group title="类型" class="way" v-model="employeeGiveGoods.goodsId">
      <wv-cell
        title="类型"
        is-link
        :value="vipCard.showGoodsName"
        @click.native="vipCardPickerShow = true"
      />
      <wv-picker
        :visible.sync="vipCardPickerShow"
        value-key="showGoodsName"
        :columns="vipCardColumns"
        @confirm="confirmVipCard"
      />
    </wv-group>
    <wv-group title="数量">
      <wv-cell>
        <wv-number-spinner slot="ft" :min="1" v-model="employeeGiveGoods.goodsNum" input-width="2em"
                           @change="onChange"/>
      </wv-cell>
    </wv-group>
    <wv-group title="备注">
      <wv-textarea placeholder="赠送备注" :rows="5" :max-length="255" v-model="employeeGiveGoods.remark"></wv-textarea>
    </wv-group>
    <router-link to="giveVipcardList" tag="p" class="zengsongjilu">赠送记录</router-link>
    <div class="model" v-show="isShow">
      <div class="bg"></div>
      <div class="modelContent">
        <img src="../../assets/close.png" alt="" width="15" @click="isShow=false" class="close">
        是否确认提交?提交后该手机号下自动发放消费券
        <p class="btn">
          <button class="call" @click="goSubmit()">确认</button>
          <button class="cancel" @click="isShow=false">取消</button>
        </p>
      </div>
    </div>
    <p class="offer" @click="showSubmit()">提交</p>
  </div>

</template>

<script type="text/ecmascript-6">
  import {getTiyanVipcardList, addTiyanVip} from '../../../test/unit/http'
  import {Toast} from 'we-vue'

  export default {
    name: 'giveVipcard',
    props: {},
    data() {
      return {
        isShow: false,
        employeeGiveGoods: {userName: '', userMobile: '', goodsNum: 1, goodsId: '', remark: ''},
        vipCardPickerShow: false,
        vipCard: [{goodsName: ''}],
        vipCardColumns: [{
          values: []
        }]
      };
    },
    methods: {
      onChange(val) {
        this.employeeGiveGoods.goodsNum = val
      },
      confirmVipCard(picker) {
        let vipCard = picker.getValues()[0];
        if (vipCard) {
          this.vipCard = vipCard;
          this.employeeGiveGoods.goodsId = vipCard.goodsId
        }
      },
      checkFrom() {
        let msg = {};
        if (this.$myUtils.isNull(this.employeeGiveGoods.userName)) {
          msg['result'] = false;
          msg['info'] = '请填写姓名';
          return msg;
        }
        if (this.$myUtils.isNull(this.employeeGiveGoods.userMobile)) {
          msg['result'] = false;
          msg['info'] = '请填写手机号';
          return msg;
        }
        if (this.$myUtils.isNull(this.employeeGiveGoods.goodsId)) {
          msg['result'] = false;
          msg['info'] = '请选择体验卡';
          return msg;
        }
        msg['result'] = true;
        msg['info'] = '校验成功';
        return msg;
      },
      getTiyanVipcardListData() {
        getTiyanVipcardList({}).then(res => {
          console.info('res', res);
          if (res.code = 100) {
            let vipCardList = res.object.vipCardList;
            for (let i in vipCardList) {
              let vipCard = vipCardList[i];
              vipCard['showGoodsName'] = vipCard.goodsName + '(剩余可赠送' + vipCard.giveGoodsNum + ')'
            }
            if (vipCardList.length > 0) {
              this.vipCardColumns[0].values = vipCardList;
              let vipCard = vipCardList[0];
              this.vipCard = vipCard;
              this.employeeGiveGoods.goodsId = vipCard.goodsId
            }
          } else {
            Toast.text(res.info)
          }
        })
      },
      showSubmit() {
        let checkResult = this.checkFrom();
        if (!checkResult.result) {
          Toast.text(checkResult.info);
          return
        }
        this.isShow = true
      },
      goSubmit() {
        let checkResult = this.checkFrom();
        if (!checkResult.result) {
          Toast.text(checkResult.info);
          return
        }
        addTiyanVip(this.employeeGiveGoods).then(res => {
          console.info('res', res.code);
          console.info('res', JSON.stringify(res));
          if (res.code == 100) {
            setTimeout(() => {
              this.$router.push("index")
            }, 1500)
          } else {
            this.isShow = false;
            Toast.text({
              duration: 1000,
              message: res.info
            })
          }
        })
      }
    },
    created() {
      this.getTiyanVipcardListData()
    },
    mounted() {

    },
    filters: {},
    components: {}
  }
</script>


<style scoped lang="less" rel="stylesheet/less">
  .zengsongjilu {
    color: #f98e3c;
  }

  .beizhus {
    padding-bottom: 60px;
  }

  .bg {
    background-color: rgba(0, 0, 0, 0.3);
    width: 100%;
    height: 100%;
    position: fixed;
    top: 0;
    left: 0;
    bottom: 50px;
  }

  .model .modelContent {
    background-color: #fff;
    border-radius: 8px;
    position: fixed;
    left: 50%;
    top: 50%;
    margin-left: -43%;
    margin-top: -111px;
    z-index: 2;
    padding: 30px 10px 20px;
    width: 80%;
    font-size: 16px;
    color: #111;

    .close {
      position: absolute;
      right: 10px;
      top: 10px
    }

    div {
      text-align: left;
      width: 75%;
      margin: 20px auto;
    }

    .list {
      font-size: 15px;
      margin-bottom: 20px;

      li:first-child {
        color: #56a7ed;
        margin-bottom: 5px;

        img {
          vertical-align: middle;
          margin-right: 5px;
        }
      }
    }

    .btn {
      button {
        height: 42px;
        line-height: 42px;
        color: #fff;
        outline: none;
        border: none;
        width: calc(50% - 20px);
        border-radius: 8px;
      }

      :first-child {
        background-color: #ccc;
        margin-right: 20px;
      }

      :last-child {
        background-color: #fc8c50;
      }

      .call {
        background-color: #fc8c50;
      }

      .cancel {
        background-color: #ccc;
      }
    }
  }

  .offer {
    position: fixed;
    bottom: 0;
    width: 100%;
    background-color: #f98e3c;
    height: 50px;
    line-height: 50px;
    color: #fff;
    z-index: 2;
  }

</style>
